package com.winsomely.leecode;

/**
 * 两数之和 II - 输入有序数组
 * 
 * @author liujiawei
 *
 */
public class TwoSum {

	// public int[] twoSum(int[] numbers, int target) {
	// int[] result = new int[2];
	// for (int i = 0; i < numbers.length; i++) {
	// for (int j = i + 1; j < numbers.length; j++) {
	// if (numbers[i] + numbers[j] == target) {
	// result[0] = i + 1;
	// result[1] = j + 1;
	// break;
	// }
	// }
	// }
	// return result;
	// }

	public int[] twoSum(int[] numbers, int target) {
		int[] result = new int[2];
		for (int i = 0, j = numbers.length - 1; i < j;) {
			if (numbers[i] + numbers[j] < target) {
				i++;
			} else if (numbers[i] + numbers[j] > target) {
				j--;
			} else {
				result[0] = i + 1;
				result[1] = j + 1;
				return result;
			}
		}
		return result;
	}

	public static void main(String[] args) {
		TwoSum t = new TwoSum();
		int[] numbers = { 2, 7, 11, 165, 167, 435 };

		int[] result = t.twoSum(numbers, 9);
		for (int i = 0; i < result.length; i++) {
			System.out.println(result[i]);
		}
	}

}
